﻿using CPPEI.BD.VMTool.Untils;
using CPPEI.BD.VMTool.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.BD.VMTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadSolutionConfig();
        }

        private void _ctlBtnEdit_Click(object sender, RoutedEventArgs e)
        {
            SolutionConfigWindow solutionConfigWindow = new SolutionConfigWindow();
            solutionConfigWindow.Closed += solutionConfigWindow_Closed;
            solutionConfigWindow.ShowDialog();
        }

        void solutionConfigWindow_Closed(object sender, EventArgs e)
        {
            LoadSolutionConfig();
        }

        private void LoadSolutionConfig()
        {
           List<Datas.Solution> solutions = VMSolutionUntil.GetAllSolution();
           if (solutions != null)
           {
               _ctlWplSolutions.Children.Clear();
               foreach (Datas.Solution solution in solutions)
               {
                   Button solutionButton = new Button();
                   solutionButton.Margin = new Thickness(2);
                   solutionButton.Content = solution.Caption;
                   solutionButton.Width = 100;
                   solutionButton.Height = 50;
                   solutionButton.DataContext = solution;
                   solutionButton.Click += solutionButton_Click;
                   _ctlWplSolutions.Children.Add(solutionButton);
               }
           }
           else
           {
               _ctlWplSolutions.Children.Clear();
           }
        }

        void solutionButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            Datas.Solution solution =  button.DataContext as Datas.Solution;
            if (solution != null)
            {
                if (App.ISFORWARDMODE)
                {
                    string targetIP = System.Configuration.ConfigurationManager.AppSettings["TargetIP"];
                    int targetPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TargetPort"]);
                    // 发送到agent程序，让agent程序转发
                    Untils.VMAgentUtil.SendToAgent(targetIP, targetPort, solution);
                }
                else
                {
                    string targetIP = System.Configuration.ConfigurationManager.AppSettings["TargetIP"];
                    string targetPort = System.Configuration.ConfigurationManager.AppSettings["TargetPort"];
                    // 直接发送到视频切换设备
                    Untils.VMUdpUntil.SendPointTarget(targetIP, targetPort, solution);
                }
            }
        }

        private void _ctlbtnCurrentTest_Click_1(object sender, RoutedEventArgs e)
        {
            VideoMatrixCurrentWindow videoMatrixCurrentWindow = new VideoMatrixCurrentWindow();
            videoMatrixCurrentWindow.ShowDialog();
        }
    }
}
